package demo6;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-19
 * Time: 22:34
 */

import java.util.ArrayList;
import java.util.Stack;


/**
 * 表达式求值
 */
public class Main {
    public int solve (String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isDigit(c)) {
                int num = 0;
                int j = i;
                while (j < chars.length && Character.isDigit(chars[j])) {
                    num = num * 10 + (chars[j++] - '0');
                }
                i = j-1;
            } else {
                if (c == '(') {
                    stack1.push(c);
                } else if (c == ')') {
                    while (!stack1.isEmpty()) {
                        if (stack1.peek()!='(') {
                            calc(stack1, stack2);
                        } else {
                            stack1.pop();
                            break;
                        }
                    }
                } else {
                    while (!stack1.isEmpty() && stack1.peek() != '(') {
                        char p = stack1.peek();
                        if (p == '*' || c != '*') {
                            calc(stack1, stack2);
                        } else {
                            break;
                        }
                    }
                    stack1.push(c);
                }
            }
        }
        while (!stack1.isEmpty() && stack1.peek() != '(') {
            calc(stack1, stack2);
        }
        return stack2.peek();
    }

    private void calc(Stack<Character> stack1, Stack<Integer> stack2) {
        if (stack1.isEmpty() || stack2.isEmpty() || stack2.size() < 2) {
            return;
        }
        int num2 = stack2.pop();
        int num1 = stack2.pop();
        char p = stack1.pop();
        if (p == '*') {
            stack2.push(num1 * num2);
        } else if (p == '+'){
            stack2.push(num1 + num2);
        } else {
            stack2.push(num1 - num2);
        }
    }
    public ArrayList<Integer> function(String s, int index){
        Stack<Integer> stack = new Stack<Integer>();
        int num = 0;
        char op = '+';
        int i;
        for(i = index; i < s.length(); i++){
            //数字转换成int数字
            //判断是否为数字
            if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                num = num * 10 + s.charAt(i) - '0';
                if(i != s.length() - 1)
                    continue;
            }
            //碰到'('时，把整个括号内的当成一个数字处理
            if(s.charAt(i) == '('){
                //递归处理括号
                ArrayList<Integer> res = function(s, i + 1);
                num = res.get(0);
                i = res.get(1);
                if(i != s.length() - 1)
                    continue;
            }
            switch(op){
                //加减号先入栈
                case '+':
                    stack.push(num);
                    break;
                case '-':
                    //相反数
                    stack.push(-num);
                    break;
                //优先计算乘号
                case '*':
                    int temp = stack.pop();
                    stack.push(temp * num);
                    break;
            }
            num = 0;
            //右括号结束递归
            if(s.charAt(i) == ')')
                break;
            else
                op = s.charAt(i);
        }
        int sum = 0;
        //栈中元素相加
        while(!stack.isEmpty())
            sum += stack.pop();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(sum);
        temp.add(i);
        return temp;
    }
    public int solve2 (String s) {
        ArrayList<Integer> res = function(s, 0);
        return res.get(0);
    }
}
